package com.bt.service.impl;

import com.bt.dto.YddArticleListDTO;
import com.bt.entity.YddArticle;
import com.bt.mapper.YddArticleMapper;
import com.bt.mapper.YddCateMapper;
import com.bt.result.PageResult;
import com.bt.service.YddArticleService;
import com.bt.vo.YddArticlePageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author 青
 * @Date 2023-05-02 11:38
 */
@Service
@Slf4j
public class YddArticleServiceImpl implements YddArticleService {
    @Autowired
    YddArticleMapper yddArticleMapper;
    @Autowired
    YddCateMapper yddCateMapper;


    /**
     * 文章列表分页查询
     *
     * @param yddArticleListDTO
     * @return
     */
    @Override
    public PageResult list(YddArticleListDTO yddArticleListDTO) {
        PageHelper.startPage(yddArticleListDTO.getPage(), yddArticleListDTO.getLimit());

        Page<YddArticlePageVO> articleVOPage = yddArticleMapper.page(yddArticleListDTO);

        return new PageResult(articleVOPage.getTotal(), articleVOPage.getPages(),
                articleVOPage.getPageSize(), articleVOPage.getPageNum(),
                articleVOPage.getResult());
    }

    /**
     * 根据id删除文章
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        yddArticleMapper.deleteById(id);
    }

    /**
     * 文章添加
     *
     * @param yddArticle
     */
    @Override
    public void add(YddArticle yddArticle) {

        //数据补全
        yddArticle.setDefaultData("1");

        yddArticle.setStatus(2);

        long time = System.currentTimeMillis();

        yddArticle.setCreate_date(time);

        yddArticle.setUpdate_date(time);

        yddArticleMapper.insert(yddArticle);
    }

    /**
     * 根据id查看文章详情(数据回显)
     *
     * @param id
     * @return
     */
    @Override
    public YddArticle rowInfo(String id) {
        YddArticle yddArticle = yddArticleMapper.findByid(id);


        return yddArticle;
    }

    /**
     * 文章修改
     *
     * @param yddArticle
     */
    @Override
    public void update(YddArticle yddArticle) {

        yddArticle.setDefaultData("1");

        yddArticle.setStatus(2);

        long time = System.currentTimeMillis();

        yddArticle.setUpdate_date(time);

        yddArticleMapper.update(yddArticle);

    }
}
